﻿@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }
<style type="text/css">
    .div-collapse {
        float: left;
        height: 20px;
        line-height: 20px;
        padding-left: 5px;
    }
</style>
<form class="layui-form layui-form-pane" action="" id="_formid">
    <div class="layui-form-item">
        <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
    </div>
</form>
<script>
    layui.use(["treeGrid", "exLayer", "exUtils", "miniPage"], function () {
        let treeGrid = layui.treeGrid;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        var openWH = miniPage.getOpenWidthHeight();
        let $ = layui.$;

        treeGrid.render({
            elem: "#tableId",
            url: "/SysBasic/Module/GetList",
            cellMinWidth: 100,
            treeId: 'Id',   //树形id字段名称
            treeUpId: 'ParentId',   //树形父id字段名称
            treeShowName: 'FullName',   //以树形式显示的字段
            defaultToolbar: layui.device().mobile ? [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-bg-blue layui-icon-search' }, { title: '显示所有数据', layEvent: 'refresh', icon: 'layui-bg-gray layui-icon-refresh' }] : [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-bg-blue layui-icon-search' }, { title: '显示所有数据', layEvent: 'refresh', icon: 'layui-bg-gray layui-icon-refresh' }, 'filter'],
            toolbar: "#toolbarTpl",
            cols: [[
                { type: "numbers", title: "NO.", fixed: 'left' },
                { field: "FullName", title: "菜单名称", width: layui.device().mobile ? 150 : null },
                { field: "UrlAddress", title: "菜单路径", width: layui.device().mobile ? 200 : null },
                { field: "RoleStr", title: "角色权限", width: layui.device().mobile ? 200 : null },
                { field: "Icon", title: "图标", width: 80, templet: "#T_Icon", align: "center" },
                { field: "OrderNo", title: "排序", width: 80, align: "center" },
                { title: "操作", width: 120, align: "center", fixed: layui.device().mobile ? null : 'right', templet: "#operationTpl" }
            ]]
        });

        $('#add').on('click', function () {
            add();
        });

        treeGrid.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.Id);//field Id 和 数据库表字段 Id 要一致
                    break;
                case "del":
                    del(data.Id);
                    break;
            }
        });

        treeGrid.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
            }
        });

        function add() {
            exLayer.openMiddle("新建菜单", "/SysBasic/Module/Add", '500px', '500px', layui.device().mobile);
        }

        function edit(id) {
            exLayer.openMiddle("编辑菜单", "/SysBasic/Module/Edit/" + id, '500px', '500px', layui.device().mobile);
        }

        function del(id) {
            exUtils.ajax("/SysBasic/Module/GetLowerLevelStr", "get", { id: id }, true).done(function (response) {
                layer.msg(response.extra, { icon: 5, time: 20000, btn: ['关闭'] });
                var _show = '确定要删除吗？';
                if (response.extra != '') {
                    _show = '该菜单还有下级菜单，该操作将删除所有下级菜单。确定要继续吗？';
                }
                exLayer.confirm(_show, function () {
                    exUtils.ajax("/SysBasic/Module/Delete", "post", { id: id, idsStr: response.extra }, true).done(function (response) {
                        exUtils.tableSuccessMsg(response.message);
                        //没开启分页，没确定按钮，手动刷新
                        setTimeout(function () {
                            window.location.reload();
                        }, 1500);
                    }).fail(function (error) {
                        console.log(error);
                    });
                })

            }).fail(function (error) {
                console.log(error);
            });
        }
    })
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="T_Icon">
    {{#  if(d.Icon == ''){ }}
    <span><i class="fa fa-circle-thin"></i></span>
    {{#  } else{ }}
    <span><i class="fa {{d.Icon}}"></i></span>
    {{#  } }}
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" id="edit">编辑</a>
    <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" id="del">删除</a>
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <div class="layui-btn-container">
        <button type='button' class="layui-btn layui-btn-normal layui-btn-sm" id="add"><i class="fa fa-plus"></i>新建菜单</button>
    </div>
</script>